package cn.ibizlab.odoo.core.odoo_bus.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Ibus_presence;
import cn.ibizlab.odoo.core.odoo_bus.domain.Bus_presence;
import cn.ibizlab.odoo.core.odoo_bus.filter.Bus_presenceSearchContext;
import cn.ibizlab.odoo.core.odoo_bus.service.IBus_presenceService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[用户上线] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Bus_presenceServiceImpl implements IBus_presenceService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Ibus_presenceClientService bus_presenceClientService;

    @Override
    public Bus_presence get(Integer id) {
        Ibus_presence clientModel = bus_presenceClientService.createModel();
        clientModel.setId(id);
        bus_presenceClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Bus_presence get(Bus_presence bus_presence) {
        Bus_presence retbus_presence = this.get(bus_presence.getId()) ;
    	BeanCopier copier=BeanCopier.create(retbus_presence.getClass(), bus_presence.getClass(), false);
        copier.copy(retbus_presence, bus_presence, null);
        return bus_presence ;
    }

    @Override
    public void updateBatch(List<Bus_presence> bus_presences, int batchSize) {

        //bus_presenceClientService.updateBatch(bus_presences,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Ibus_presence clientModel = bus_presenceClientService.createModel();
        clientModel.setId(id);
        bus_presenceClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void createBatch(List<Bus_presence> bus_presences, int batchSize) {

                        //bus_presenceClientService.createBatch(bus_presences,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //bus_presenceClientService.removeBatch(bus_presences) ;

    }

    @Override
    public boolean update(Bus_presence bus_presence) {
        boolean bOk = false ;
        Ibus_presence clientModel = this.convert2Model(bus_presence,null);
        bus_presenceClientService.update(clientModel) ;
        this.convert2Domain(clientModel,bus_presence) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean create(Bus_presence bus_presence) {
        boolean bOk = false ;
        Ibus_presence clientModel = this.convert2Model(bus_presence,null);
        bus_presenceClientService.create(clientModel) ;
        this.convert2Domain(clientModel,bus_presence);
        bOk = true ;

        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Bus_presence> searchDefault(Bus_presenceSearchContext context) {
        List<Bus_presence> list = new ArrayList<Bus_presence>();
        Page<cn.ibizlab.odoo.core.client.model.Ibus_presence> clientModelList = bus_presenceClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ibus_presence clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Bus_presence>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据用户查询
     */
    @Override
    public Page<Bus_presence> selectByOdoo_user(Integer id){
        Bus_presenceSearchContext context = new Bus_presenceSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Bus_presence> list = new ArrayList<Bus_presence>();
        Page<cn.ibizlab.odoo.core.client.model.Ibus_presence> clientModelList = bus_presenceClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ibus_presence clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Bus_presence>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Ibus_presence convert2Model(Bus_presence domain , Ibus_presence model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = bus_presenceClientService.createModel() ;
        }

         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getLast_presenceDirtyFlag())
            model.setLast_presence(domain.getLast_presence());
         if(domain.getLast_pollDirtyFlag())
            model.setLast_poll(domain.getLast_poll());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getStatusDirtyFlag())
            model.setStatus(domain.getStatus());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getUser_id_textDirtyFlag())
            model.setUser_id_text(domain.getUser_id_text());
         if(domain.getUser_idDirtyFlag())
            model.setUser_id(domain.getUser_id());
        return model ;
    }

    public Bus_presence convert2Domain( Ibus_presence model ,Bus_presence domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Bus_presence();
        }

        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getLast_presenceDirtyFlag())
            domain.setLast_presence(model.getLast_presence());
        if(model.getLast_pollDirtyFlag())
            domain.setLast_poll(model.getLast_poll());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getStatusDirtyFlag())
            domain.setStatus(model.getStatus());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getUser_id_textDirtyFlag())
            domain.setUser_id_text(model.getUser_id_text());
        if(model.getUser_idDirtyFlag())
            domain.setUser_id(model.getUser_id());
        return domain ;
    }


}
